﻿/**
 * skfjslf
 */
using FormulaRunning.DAL;
using FormulaRunning.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.Entity;
using FormulaRunning.Utils;

namespace FormulaRunning.Controllers
{
    public class HomeController : Controller
    {
        private FormulaContext db = new FormulaContext();

        // 生成公式列表
        public ActionResult Index()
        {
            var formulas = db.Formulas;
            return View(formulas.ToList());
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

        public ActionResult Help()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        public ActionResult Create()
        {
            ViewBag.Tags = db.Tags.ToArray<Tag>();
            return View();
        }

        //
        // GET: /Formula/Details/5

        public ActionResult Details(int id = 0)
        {
            Formula formula = db.Formulas.Find(id);
            if (formula == null)
            {
                return HttpNotFound();
            }
            return View(formula);
        }

        //
        // POST: /Formula/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize]
        public ActionResult Create(FormCollection formCollection)
        {
            Formula formula = new Formula();
            ICollection<Tag> tags = new HashSet<Tag>();
            formula.UserName = User.Identity.Name;
            formula.SubmitTime = DateTime.Now;
            UpdateModel(formula, new string[] { "Title", "Description", "FormulaString" });
            if (TryUpdateModel(formula, new string[] { "Title", "Description", "FormulaString" }))
            {
                string[] tagIds = formCollection.GetValues("TagName");

                foreach (var tagId in tagIds) {
                    if (!StringUtil.IsNum(tagId))
                    {
                        continue;
                    }
                    int intTagId = int.Parse(tagId);
                    var tag = from t in db.Tags where t.TagID == intTagId select t;
                    if (tag == null)
                    {
                        // tag = new Tag { TagName = };
                    }
                    tags.Add(tag.Single());
                    //TODO 如果tag为空，则新增tag
                }
                if (tags.Count > 0) {
                    formula.Tags = tags;
                }
                try
                {
                    if (ModelState.IsValid)
                    {
                        db.Formulas.Add(formula);
                        db.SaveChanges();
                        return RedirectToAction("Index");
                    }
                }
                catch (DataException)
                {
                    ModelState.AddModelError("", "unable to save the changes");
                }
            }
            ViewBag.Tags = db.Tags.ToArray<Tag>();
            return View(formula);
        }
    }
}
